package com.bitbakery.plugin.cl.lang.psi;

import com.bitbakery.plugin.cl.lang.CLElementVisitor;
import com.bitbakery.plugin.cl.lang.CLFileType;
import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.scope.PsiScopeProcessor;
import org.jetbrains.annotations.NotNull;

public class CLFile extends PsiFileBase {
    public CLFile(FileViewProvider fileViewProvider) {
        super(fileViewProvider, CLFileType.COMMON_LISP.getLanguage());
    }

    @NotNull
    public FileType getFileType() {
        return CLFileType.COMMON_LISP;
    }

    public CLElement getRootElement() {
        return (CLElement) getFirstChild();
    }

    public String toString() {
        return "CLFile:" + getName();
    }

    public boolean processDeclarations(PsiScopeProcessor processor,
                                       PsiSubstitutor substitutor,
                                       PsiElement lastParent,
                                       PsiElement place) {
        final PsiElement[] children = getChildren();
        for (PsiElement child : children) {
            if (child == lastParent) {
                break;
            }
            if (!child.processDeclarations(processor, substitutor, lastParent, place)) {
                return false;
            }
        }
        return true;
    }

    public void accept(PsiElementVisitor visitor) {
        if (visitor instanceof CLElementVisitor) {
            ((CLElementVisitor) visitor).visitCommonLispElement(this);
        } else {
            super.accept(visitor);
        }
    }
}
